package com.enterprise.projectManager.service.implemention;

import com.enterprise.projectManager.model.Log;
import com.enterprise.projectManager.rope.ContractRope;
import com.enterprise.projectManager.rope.LogRope;
import com.enterprise.projectManager.rope.ProblemRope;
import com.enterprise.projectManager.rope.ProjectRope;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

@Service
@RequiredArgsConstructor
@Transactional
@Slf4j
public class IndexServiceImpl{
    private final ProjectRope projectRope;
    private final ContractRope contractRope;
    private final LogRope logRope;
    private final ProblemRope problemRope;
    private final LogImpl logService;
    public int getProjectCount()
    {
        return projectRope.findAll().size();
    }
    public int getContractCount()
    {
        return contractRope.findAll().size();
    }
    public double calculateCompletionPercentage() {
        long totalProjects = projectRope.count();
        if (totalProjects == 0) {
            return 0.0; // 避免除零错误
        }
        long finishedProjects = projectRope.countByProjectState("已竣工");

        Double ratio = ((double) finishedProjects / totalProjects) * 100.0;
        DecimalFormat decimalFormat = new DecimalFormat("0.##");
        String formattedNumber = decimalFormat.format(ratio);
        return Double.parseDouble(formattedNumber);
    }
    public int delayProjectCount()
    {
        return projectRope.countByProjectState("已延期").intValue();
    }
    public int delayLogToday()
    {
        LocalDateTime startOfDay = LocalDate.now().atStartOfDay();
        LocalDateTime now = LocalDateTime.now();
        Date date1 = Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
        Date date2 = Date.from(now.atZone(ZoneId.systemDefault()).toInstant());
        return logRope.findByLogBeyondDateBetween(date1,date2).size();
    }
    public int newProblemToday()
    {
        LocalDateTime startOfDay = LocalDate.now().atStartOfDay();
        LocalDateTime now = LocalDateTime.now();
        Date date1 = Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
        Date date2 = Date.from(now.atZone(ZoneId.systemDefault()).toInstant());
        return problemRope.findByProblemSenddateBetween(date1,date2).size();
    }
    public Map<String,Object> getIndexData()
    {
        Map<String,Object> data = new HashMap<>();
        data.put("contractCount",getContractCount());
        data.put("projectCount",getProjectCount());
        data.put("projectProgress",calculateCompletionPercentage());
        data.put("delayProjectCount",delayProjectCount());
        data.put("delayLogToday",delayLogToday());
        data.put("newProblemToday",newProblemToday());
        System.out.println(data);
        return data;
    }

    //项目整体建设进度
    public List<Long> getProjectBuildingProcess()
    {
        long state1 = projectRope.countByProjectState("未开始");
        long state2 = projectRope.countByProjectState("进行中");
        long state3 = projectRope.countByProjectState("已延期");
        long state4 = projectRope.countByProjectState("已竣工");
//        Map<String,Long> data = new HashMap<>();
//        data.put("notStart",state1);
//        data.put("inProcess",state2);
//        data.put("postponed",state3);
//        data.put("completed",state4);
        List<Long> data = new ArrayList<>();
        data.add(state1);
        data.add(state2);
        data.add(state3);
        data.add(state4);
        return data;
    }


    //超期项目占比
    public List<Double> getPostponedRatio()
    {
        List<Double> data =new ArrayList<>();
        long postponed= projectRope.countByProjectState("已延期");
        long all = projectRope.findAll().size();
        double ratio = (double) postponed/all;
        // 创建 DecimalFormat 对象并指定格式化模式为 "0.##"（表示保留两位小数）
        ratio = ratio * 100.00;
        DecimalFormat decimalFormat = new DecimalFormat("0.##");
        String formattedNumber = decimalFormat.format(ratio);
        data.add(Double.parseDouble(formattedNumber));
        return data;
    }

    //项目阶段分类情况
    public List<Long> getProjectBuildingPhaseRatio()
    {
        long phase1 = projectRope.countByProjectCurrentPhase("方案设计");
        long phase2 = projectRope.countByProjectCurrentPhase("基层处理");
        long phase3 = projectRope.countByProjectCurrentPhase("面层处理");
        long phase4 = projectRope.countByProjectCurrentPhase("深层处理");
        long phase5 = projectRope.countByProjectCurrentPhase("项目验收");
        long phase6 = projectRope.countByProjectCurrentPhase("项目收尾");
        List<Long> data = new ArrayList<>();
        data.add(phase1);
        data.add(phase2);
        data.add(phase3);
        data.add(phase4);
        data.add(phase5);
        data.add(phase6);
//        data.put("schemeDesign",phase1);
//        data.put("basicTreatment",phase2);
//        data.put("surfaceTreatment",phase3);
//        data.put("deepTreatment",phase4);
//        data.put("projectAcceptance",phase5);
//        data.put("projectClosure",phase6);
        return data;
    }
}
